﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using VisionCloud.Domain.Common;
using VisionCloud.Domain.DtoModel.Ms;
using VisionCloud.Service.Ms;
using VisionCloud.Utility;

namespace VisionCloud.Api.Controllers.Ms
{
    /// <summary>
    /// 负责接收版本文件上传
    /// </summary>
    [ApiVersion("1.0")]
    [Route("api/ms/[controller]/[Action]")]
    public class FileController : ApiBaseController
    {
        public FileService FileService { get; set; }



        #region 上传文件
        /// <summary>
        /// 多个文件的上传
        /// </summary>
        /// <param name="formCollection"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> upload(IFormCollection formCollection)
        {
            //todo formCollection对象在这里应该转对实体传到下一层
            return  TryCatch(() => { FileService.UploadFilesAsync(formCollection, _tokenModel); });
        }


        /// <summary>
        /// 机器人的上传需要机器人令牌验证不验证用户登录信息
        /// </summary>
        /// <param name="formCollection"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> RobotUpload(IFormCollection formCollection)
        {
            //todo formCollection对象在这里应该转对实体传到下一层
            return  TryCatch(() => { FileService.RobotUploadFilesAsync(formCollection, _tokenModel); });
        }
        /// <summary>
        /// 上传并保存
        /// </summary>
        /// <param name="files"></param>
        /// <param name="sbMD5"></param>
        /// <param name="os"></param>
        /// <param name="version"></param>
        /// <param name="mode"></param>
        /// <param name="notes"></param>
        /// <returns></returns>
        private async Task<ApiResult> UploadAndSaveAsync(IFormCollection formCollection)
        {
            //todo formCollection对象在这里应该转对实体传到下一层
            return  TryCatch(() => { FileService.UploadAndSaveAsync(formCollection, _tokenModel); });
        }



        #endregion

        #region 下载文件

        /// <summary>
        /// 机器人下载文件，验证ID和令牌
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult robotdownload(IFormCollection formCollection)
        {
            //todo formCollection对象在这里应该转对实体传到下一层
            var result = FileService.RobotDownload(formCollection);

            //获取文件的ContentType
            var provider = new FileExtensionContentTypeProvider();
            var memi = provider.Mappings[result.Result.Item3];
            return File(result.Result.Item1, memi, Path.GetFileName(result.Result.Item2));
        }

        /// <summary>
        /// 用户下载文件
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult download(QueryDownload queryDownload)
        {
            var result = FileService.Download(queryDownload);

            //获取文件的ContentType
            var provider = new FileExtensionContentTypeProvider();
            var memi = provider.Mappings[result.Result.Item3];
            return File(result.Result.Item1, memi, Path.GetFileName(result.Result.Item2));
        }

        #endregion
    }

}
